package download;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class DownLoadFile {
	/**
	 * 根据 url 和网页类型生成需要保存的网页的文件名 去除掉 url 中非文件名字符
	 */
	public String getFileNameByUrl(String url, String contentType) {
		// remove http://
		url = url.substring(7);
		// text/html类型
		if (contentType.indexOf("html") != -1
				|| contentType.indexOf("text") != -1) {
			url = url.replaceAll("[\\?/:*|<>\"]", "_") + ".html";
			return url;
		}
		// 如application/pdf类型
		else {
			return url.replaceAll("[\\?/:*|<>\"]", "_") + "."
					+ contentType.substring(contentType.lastIndexOf("/") + 1);
		}
	}

	public boolean canParse(String contentType) {

		// 可以被解释的情况,html,text,json之类
		if (contentType.indexOf("html") != -1
				|| contentType.indexOf("text") != -1) {

			return true;
		}
		// 如application/pdf类型
		else {
			return false;
		}
	}

	/**
	 * 保存网页字节数组到本地文件 filePath 为要保存的文件的相对地址
	 */
	private String saveToLocal(InputStream inputStream, String filePath,
			boolean store) {
		StringBuffer buffer = new StringBuffer();
		try {

			OutputStream out = new FileOutputStream(filePath);

			int c;
			while ((c = inputStream.read()) != -1) {
				if (store)
					buffer.append(c);
				out.write(c);
			}
			out.flush();
			out.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (store)
			return buffer.toString();

		return null;
	}

	/* 下载 url 指向的网页 */
	public String downloadFile(String url) {
		String webContent = null;
		String filePath = null;
		/* 1.生成 HttpClinet 对象并设置参数 */
		HttpClient httpClient = new HttpClient();
		// 设置 Http 连接超时 5s
		httpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(5000);
		//读取超时
		//httpClient.getHttpConnectionManager().getParams().setSoTimeout(6000);
		/* 2.生成 GetMethod 对象并设置参数 */
		GetMethod getMethod = new GetMethod(url);

		// 设置 get 请求超时 5s
		getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
		// 设置请求重试处理设置请求,用的是默认的重试处理：请求三次
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		
		/* 3.执行 HTTP GET 请求 */
		try {

			int statusCode = httpClient.executeMethod(getMethod);
			// 判断访问的状态码
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: "
						+ getMethod.getStatusLine());
				filePath = null;
			}

			/* 4.处理 HTTP 响应内容 */
			InputStream inputStream = getMethod.getResponseBodyAsStream();// 读取为字节数组
			// 根据网页 url 生成保存时的文件名
			filePath = "temp/"
					+ getFileNameByUrl(url,
							getMethod.getResponseHeader("Content-Type")
									.getValue());

			if (canParse(getMethod.getResponseHeader("Content-Type").getValue())) {
				webContent = saveToLocal(inputStream, filePath, true);
			} else {
				webContent = saveToLocal(inputStream, filePath, false);
			}

		} catch (HttpException e) {
			// 发生致命的异常，可能是协议不对或者返回的内容有问题
			System.out.println("发生致命性错误");
			e.printStackTrace();
		} catch (IOException e) {
			// 发生网络异常
			System.out.println("网络异常");
		} finally {
			// 释放连接
			getMethod.releaseConnection();
		}
		return webContent;

	}

}